<template>
  <div class="page_box">
    <div class="tips_box">
      <div class="tips_item" v-for="item in nodeType" :key="item.name">
        <div class="tips_cicle" :style="{ background: item.color }"></div>
        <div class="tips_name">{{ item.name }}</div>
      </div>
    </div>
    <div id="jsmind_container" ref="jsmind"></div>
  </div>
</template>

<script>
import { mockData } from "./mock";
import "jsmind/style/jsmind.css"; // 这样引入的样式 是全局样式

import jsMind from "jsmind/js/jsmind.js";
export default {
  data() {
    return {
      nodeType: [
        {
          name: "事件类别",
          color: "#3375f6",
          expanded: true,
        },
        {
          name: "预警测点",
          color: "#388145",
          expanded: true,
        },
        {
          name: "关联模型",
          color: "#84588e",
          expanded: true,
        },
        {
          name: "关联相关量测点",
          color: "#1c407d",
          expanded: false,
        },
        {
          name: "关联相关分析图",
          color: "#bb8f42",
          expanded: true,
        },

        {
          name: "展示图元",
          color: "#bb8e42",
          expanded: true,
        },
        {
          name: "相关量测点",
          color: "#1c478d",
          expanded: true,
        },
      ],
    };
  },

  methods: {
    init() {
      var options = {
        // options 将在下一章中详细介绍
        container: "jsmind_container", // [必选] 容器的ID，或者为容器的对象
        editable: true, // [可选] 是否启用编辑
        theme: "orange", // [可选] 主题
      };
      var jm = new jsMind(options);

      const mockList = [mockData];
      this.formatData(mockList);

      console.log("mockList", mockList);

      const configData = {
        /* 元数据，定义思维导图的名称、作者、版本等信息 */
        meta: {
          name: "jsMind-demo-tree",
          author: "hizzgdev@163.com",
          version: "0.2",
        },
        /* 数据格式声明 */
        format: "node_tree",
        /* 数据内容 */
        data: mockList[0],
      };

      jm.show(configData);
    },
    // 递归处理树结构
    formatData(mocktree) {
      mocktree.forEach((item) => {
        // 处理 id
        item.id = this.generateUUID();
        // 处理其他默认配置
        const type = this.nodeType.find((node) => node.name === item.type);
        if (type) {
          item.expanded = type.expanded;
          item["background-color"] = type.color;
        }
        if (item.children && item.children.length > 0) {
          this.formatData(item.children);
        } else {
          return;
        }
      });
    },
    // 获取 uuid
    generateUUID() {
      let d = new Date().getTime();
      let uuid = "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(
        /[xy]/g,
        function (c) {
          let r = (d + Math.random() * 16) % 16 | 0;
          d = Math.floor(d / 16);
          return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
        }
      );
      return uuid;
    },
  },
  mounted() {
    this.init();
  },
};
</script>

<style lang="scss" scoped>
.page_box {
  width: 100%;
  height: 700px;
  //   border: 1px solid #000;
  position: relative;

  #jsmind_container {
    width: 100%;
    height: 100%;
  }

  .tips_box {
    position: absolute;
    display: flex;
    height: 30px;
    right: 0;
    top: -35px;
    .tips_item {
      display: flex;
      align-items: center;
      margin-left: 20px;
      .tips_cicle {
        width: 20px;
        height: 20px;
        // border: 1px solid #000;
        border-radius: 50%;
        margin-right: 5px;
      }
      .tips_name {
        font-size: 18px;
      }
    }
  }
}
</style>
